<?php


namespace app\api\lists;


use app\common\enum\GoodsEnum;
use app\common\model\goods\Goods;

class GoodsLists extends BaseShopDataLists
{
    /**
     * @notes 搜索条件
     * @return array
     * @author ljj
     * @date 2022/2/17 5:18 下午
     */
    public function setSearch(): array
    {
        return array_diff(array_keys($this->params), ['page_no', 'page_size']);
    }

    /**
     * @notes 服务列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author ljj
     * @date 2022/2/17 5:17 下午
     */
    public function lists(): array
    {
        $lists = Goods::field('id,name,image,remarks,price,unit_id')
            ->append(['unit_desc'])
            ->withSearch($this->setSearch(), $this->params)
            ->where(['status'=>GoodsEnum::SHELVE])
            ->limit($this->limitOffset, $this->limitLength)
            ->select()
            ->toArray();

        foreach ($lists as &$list) {
            $list['price'] = trim(rtrim(sprintf("%.4f", $list['price'] ), '0'),'.');
        }

        return $lists;
    }

    /**
     * @notes 服务总数
     * @return int
     * @author ljj
     * @date 2022/2/17 5:17 下午
     */
    public function count(): int
    {
        return Goods::withSearch($this->setSearch(), $this->params)
            ->where(['status'=>GoodsEnum::SHELVE])
            ->count();
    }
}